<template>
  <div class="dashboard-container">
    <div class="app-container">
      <PageTools>
        <template #after>
          <el-button size="small" type="primary" @click="onClickAdd(1,'0')">
            添加权限
          </el-button>
        </template>
      </PageTools>
      <!--      表格-->
      <el-table ref="table" :data="tableData" row-key="id">
        <el-table-column label="名称" prop="name">
          <template v-slot="{row}">
            <i
              v-if="row.pid==='0'"
              class="el-icon-folder-opened"
              style="margin-right: 5px"
              @click="handleExpansion(row)"
            />
            <i
              v-else
              class="el-icon-view"
              style="margin-right: 5px"
            />
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="标识" prop="code" />
        <el-table-column align="center" label="描述" prop="description" />
        <el-table-column align="center" label="操作">
          <template v-slot="{row}">
            <el-button v-if="row.type===1" type="text" @click="onClickAdd(2,row.id)">添加</el-button>
            <el-button type="text" @click="handleEdit(row.id)">编辑</el-button>
            <el-button type="text" @click="handleDel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <el-dialog
      :title="formData.id?'编辑':'添加'"
      :visible.sync="UpdatePermissionDialogVisible"
      width="50%"
      @close="onClose"
    >
      <el-form ref="form" :model="formData" :rules="rules" label-width="80px">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" style="width:90%" />
        </el-form-item>
        <el-form-item label="权限开启">
          <el-switch
            v-model="formData.enVisible"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onClose">取 消</el-button>
        <el-button type="primary" @click="onSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  addPermission,
  delByIdPermission,
  getAllPermission,
  getByIdPermission,
  updateByIdPermission
} from '@/api/permission'
import { handleLoading, transListToTree } from '@/utils'

export default {
  name: 'Permission',
  data() {
    return {
      tableData: [],
      UpdatePermissionDialogVisible: false, // 控制添加
      formData: {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: '0'
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: 'blur' }],
        code: [{ required: true, message: '权限标识不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '权限描述不能为空', trigger: 'blur' }]
      }
    }
  },

  created() {
    this.loadAllPermission()
  },

  methods: {
    async loadAllPermission() {
      const res = await getAllPermission()
      this.tableData = transListToTree(res, '0')
    },
    handleExpansion(row) {
      row.isExpansion = !row.isExpansion
      this.$refs.table.toggleRowExpansion(row, row.isExpansion)
    },
    async onSave() {
      try {
        await this.$refs.form.validate()
        handleLoading(async() => {
          if (this.formData.id) {
            // 编辑
            await updateByIdPermission(this.formData)
            this.$message.success('编辑成功')
          } else {
            await addPermission(this.formData)
            this.$message.success('添加成功')
          }
          await this.loadAllPermission()
          this.onClose()
        }, '.el-dialog__body')
      } catch (e) {
        console.log(e)
      }
    },
    onClickAdd(type, pid) {
      this.formData.type = type
      this.formData.pid = pid
      this.UpdatePermissionDialogVisible = true
    },
    onClose() {
      this.$refs.form.resetFields()
      this.formData = {
        name: '',
        code: '',
        description: '',
        enVisible: '0',
        type: '0'
      }
      this.UpdatePermissionDialogVisible = false
    },
    async handleEdit(id) {
      this.UpdatePermissionDialogVisible = true

      this.$nextTick(() => {
        handleLoading(async() => {
          this.formData = await getByIdPermission(id)
        }, '.el-dialog__body')
      })
    },
    // 删除
    handleDel(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delByIdPermission(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        await this.loadAllPermission()
        this.onClose()
      }).catch(() => {
      })
    }
  }
}
</script>

<style scoped lang='scss'>
::v-deep .el-table__expand-icon {
  display: none !important;
}
</style>
